﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Game.Models;
using System.Text;

namespace Game.Controllers
{
    public class ConnectionController : ApiController
    {
        private static Models.Game game = new Models.Game(null);
        private List<Models.Game> games = LoadGames();

        public string StartNewGame()
        {
            Models.Game game = new Models.Game(null);
            game.SaveGame(game.Id);
            games.Add(game);
            return game.ToString();
        }

        public void AddPlayerToGame(string id, string name)
        {
            Models.Player player = new Player(name);
            foreach (Models.Game game in games)
            {
                if (game.Id == id)
                {
                    for (int i = 0; i < game.Players.Count; i++)
                    {
                        if (game.Players[i].Name != player.Name)
                        {
                            game.AddPlayer(player);
                        }
                    }
                    game.SaveGame(id);
                }
            }
        }

        public string SeeAllGames()
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < games.Count; i++)
            {
                builder.Append(games[i] + Environment.NewLine);
            }
            return builder.ToString();
        }

        public string SeeGame(string id)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < games.Count; i++)
            {
                if (games[i].Id == id)
                {
                    builder.Append(games[i]);
                }
            }
            return builder.ToString();
        }

        public static List<Models.Game> LoadGames()
        {
            List<Models.Game> games = new List<Models.Game>();
            string[] _games;
            using (StreamReader reader = new StreamReader("game.gm"))
            {
                _games = reader.ReadToEnd().Split('\n');
            }
            foreach (string game in _games)
            {
                if (game.Length > 0)
                {
                    game.Remove(game.Length - 2);
                }
            }
            for (int i = 0; i < _games.Length; i++)
            {
                string[] _game = _games[i].Split(';');
                if (_game.Length > 1)
                {
                    Models.Game game = new Models.Game(_game[0]);
                    string[] players = _game[1].Split(',');
                    for (int j = 0; j < players.Length; j++)
                    {
                        Player player = new Player(players[j]);
                        game.AddPlayer(player);
                    }
                    games.Add(game);
                }
            }
            return games;
        }
    }
}